package hz.mobileproject.pda.model.attendancemanager.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.app.MainApp;
import hz.mobileproject.pda.entity.MessageEvent;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.model.attendancemanager.bean.Rq_loadDept;
import hz.mobileproject.pda.model.attendancemanager.fragment.leave.AllLeaveDataFragment;
import hz.mobileproject.pda.model.attendancemanager.fragment.leave.MyLeaveRequestFragment;
import hz.mobileproject.pda.model.attendancemanager.fragment.leave.WaitMeApproveLeaveFragment;
import hz.mobileproject.pda.model.attendancemanager.fragment.outgoing.AllOutDataFragment;
import hz.mobileproject.pda.model.attendancemanager.fragment.outgoing.MyOutRequestFragment;
import hz.mobileproject.pda.model.attendancemanager.fragment.outgoing.WaitMeApproveFragment;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.tree.FileBean;
import hz.mobileproject.pda.ui.adapter.PopListViewAdapter;
import hz.mobileproject.pda.ui.base.BaseFragmentActivity;
import hz.mobileproject.pda.ui.view.CustomPopWindow;
import hz.mobileproject.pda.utils.HttpRqUtils;

@ContentView(R.layout.activity_leave)
public class LeaveActivity extends BaseFragmentActivity implements RadioGroup.OnCheckedChangeListener {
    @ViewInject(R.id.iv_right_menu)
    private ImageView iv_right_menu;
    @ViewInject(R.id.vp_outgoing)
    private ViewPager vp_outgoing;
    @ViewInject(R.id.rg_outgoing)
    private RadioGroup rg_outgoing;
    @ViewInject(R.id.rb1)
    private RadioButton rb1;
    @ViewInject(R.id.rb2)
    private RadioButton rb2;
    @ViewInject(R.id.rb3)
    private RadioButton rb3;

    //选项弹窗list
    private String[] mPopItems = {"请假申请"};
    private List<Fragment> mFragments = new ArrayList<>();
    private FragmentPagerAdapter mAdapter;

    @Override
    public void initData() {
        //初始化主页fragment
        mFragments.add(new AllLeaveDataFragment());
        mFragments.add(new WaitMeApproveLeaveFragment());
        mFragments.add(new MyLeaveRequestFragment());
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        vp_outgoing.setOffscreenPageLimit(3);//缓存页面个数
        vp_outgoing.setCurrentItem(0);//默认选中第一个
        vp_outgoing.setAdapter(mAdapter);
        rg_outgoing.setOnCheckedChangeListener(this);
        vp_outgoing.addOnPageChangeListener(onPageChangeListener);
        //TODO 获取部门数据
        loadDeptFromSer();
    }


    @Event({R.id.iv_right_menu})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.iv_right_menu:
                showPopWindow(mPopItems, iv_right_menu);
                break;
        }
    }


    /**
     * 加载部门数据
     */
    private void loadDeptFromSer() {
        Rq_loadDept rq = new Rq_loadDept();
        rq.setPolice_code(MainApp.getInstance().getLoginUser().getPolice_code());
        rq.setDept_id(MainApp.getInstance().getLoginUser().getDepartment_id() + "");
        rq.setFlowTypeId(Rq_loadDept.FLOW_TYPE_ID_OUT_AND_LEAVE);
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_LOAD_DEPT, rq, new CustomNetCallBack() {
            @Override
            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                List<FileBean> deptList = new Gson().fromJson(data, new TypeToken<List<FileBean>>() {
                }.getType());
                if (deptList == null || deptList.size() <= 0) {
                    ToastUtils.showShort("部门数据为空");
                    return;
                }
                EventBus.getDefault().post(new MessageEvent("LeaveActivity.loadDeptFromSer", "", deptList));
            }

            @Override
            public void onErrorFromSer(int rtnCode, String rtnMsg) {
                ToastUtils.showShort("服务器返回错误(" + rtnCode + "," + rtnMsg + ")");
            }

            @Override
            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
                ToastUtils.showShort("连接服务器失败，请检查网络");
            }
        });
    }


    /**
     * 弹出选择项popwindow
     *
     * @param
     * @param showAsDropDownView
     */
    private void showPopWindow(String[] strs, final View showAsDropDownView) {
        if (strs == null || strs.length <= 0) {
            return;
        }
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_custom_list, null);
        //处理popWindow 显示内容
        ListView listView = (ListView) contentView.findViewById(R.id.lv_dialog);
        PopListViewAdapter adapter = new PopListViewAdapter(this, strs);
        adapter.setColor("#eeeeee", "#000000");
        listView.setAdapter(adapter);
        //创建并显示popWindow
        final CustomPopWindow mListPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .size(500, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create()
                .showAsDropDown(showAsDropDownView, 0, 10);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mListPopWindow.dissmiss();
                Intent it = new Intent();
                switch (position) {
                    case 0: //请假申请
                        it.setClass(LeaveActivity.this, LeaveApplyActivity.class);
                        LeaveActivity.this.startActivity(it);
                        break;
                }
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb1:
                vp_outgoing.setCurrentItem(0, true);
                break;
            case R.id.rb2:
                vp_outgoing.setCurrentItem(1, true);
                break;
            case R.id.rb3:
                vp_outgoing.setCurrentItem(2, true);
                break;
        }
    }


    /**
     * 滑动切换页面设置radioButton
     */
    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    rb1.setChecked(true);
                    break;
                case 1:
                    rb2.setChecked(true);
                    break;
                case 2:
                    rb3.setChecked(true);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };
}
